﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechnicalIndicators.DateBased;
using TechnicalIndicators.Points;
using Utils;

namespace TechnicalIndicatorsIntegrationTests
{
	[TestClass]
	public class ExponentialAverageIT
	{
		[TestMethod]
		public void TestEMA()
		{
			DateTime startDate = DateTime.Parse("01/01/2012 09:00:00");

			ObservableCollection2<DatedSeriePoint> source = new ObservableCollection2<DatedSeriePoint>();

			ExponentialAverage ema = new ExponentialAverage();
			ema.Source = new ReadWriteLockedObservableCollection<DatedSeriePoint>(source, new TimeSpan(0, 0, 1));

			ema.Configure(10);
			ema.Start();

			source.Add(new DatedSeriePoint(startDate, 22.27m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(1), 22.19m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(2), 22.08m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(3), 22.17m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(4), 22.18m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(5), 22.13m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(6), 22.23m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(7), 22.43m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(8), 22.24m));
			Assert.IsNull(ema.LastValue);
			source.Add(new DatedSeriePoint(startDate.AddSeconds(9), 22.29m));
			Assert.AreEqual(22.22m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(10), 22.15m));
			Assert.AreEqual(22.21m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(11), 22.39m));
			Assert.AreEqual(22.24m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(12), 22.38m));
			Assert.AreEqual(22.27m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(13), 22.61m));
			Assert.AreEqual(22.33m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(14), 23.36m));
			Assert.AreEqual(22.52m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(15), 24.05m));
			Assert.AreEqual(22.80m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(16), 23.75m));
			Assert.AreEqual(22.97m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(17), 23.83m));
			Assert.AreEqual(23.13m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(18), 23.95m));
			Assert.AreEqual(23.28m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(19), 23.63m));
			Assert.AreEqual(23.34m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(20), 23.82m));
			Assert.AreEqual(23.43m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(21), 23.87m));
			Assert.AreEqual(23.51m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(22), 23.65m));
			Assert.AreEqual(23.53m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(23), 23.19m));
			Assert.AreEqual(23.47m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(24), 23.10m));
			Assert.AreEqual(23.40m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(25), 23.33m));
			Assert.AreEqual(23.39m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(26), 22.68m));
			Assert.AreEqual(23.26m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(27), 23.10m));
			Assert.AreEqual(23.23m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(28), 22.40m));
			Assert.AreEqual(23.08m, Math.Round(ema.LastValue.Y, 2));
			source.Add(new DatedSeriePoint(startDate.AddSeconds(29), 22.17m));
			Assert.AreEqual(22.92m, Math.Round(ema.LastValue.Y, 2));
		}

	}
}
